<?php
namespace app\admin\controller;
use think\Db;
use app\service\TechnService;
class Techn extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 服务列表
	 */
	public function index()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [];
				if(!empty($params['title']))
				{
					$map['real_name'] = ['like','%'.$params['title'].'%'];
				}
				
				if(!empty($params['nickname']))
				{
					$map['nickname'] = ['like','%'.$params['nickname'].'%'];
				}
                if(!empty($params['city']))
                {
                    $map['city'] = $params['city'];
                }
				if(!empty($params['mobile']))
				{
					$map['mobile'] = $params['mobile'];
				}
				// print_r($map);exit;
				
				$count = Db::name('techn')->where($map)->count();
				$list_data = Db::name('techn')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
						$list_data[$key]['save_url'] = url('Techn/saveinfo', ['id' => $value['id']]);
						//编辑url
						$list_data[$key]['sextitle'] = $value['sex']==1?'男':'女';
						
						$list_data[$key]['lhtitle'] = $value['is_lh']==1?'拉黑':'正常';
						
						if($value['work_status']==1){
						    $list_data[$key]['workmsg']='接单';
						}elseif($value['work_status']==2){
						    $list_data[$key]['workmsg']='休息';
						}else{
						    $list_data[$key]['workmsg']='下线';
						}
						$list_data[$key]['addr']=$value['province_name'].'-'.$value['city_name'].'-'.$value['county_name'];
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
            $city_list = Db::name('proxy')->where('is_delete_time',0)->select();
            $this->assign('city_list',$city_list);
			return $this->fetch();
		}
	}

	/**
	 * 服添添加/修改 页面
	 */
	public function saveinfo()
	{
		// 参数
		$params = input();
		$info = [];
		
			//查询
		$info = Db::name('techn')->where(['id' => $params['id']])->find();
			
		$this->assign('info',$info);

        $map = [
					'is_delete_time' => 0
				];
				
		$service = Db::name('service')->where($map)->select();
		
		$service_ids=explode(',',$info['service_ids']);
		
		//编辑器文件存放地址
		$this->assign('service_ids', $service_ids);
		$this->assign('service', $service);
		
		$service_class=db::name('service_class')->where('is_delete_time',0)->select();
		
		$this->assign('service_class',$service_class);
		
		$uptel=db::name('user')->where('id',$info['upid'])->value('mobile');
		$this->assign('uptel',$uptel);
		
		return $this->fetch();
	}

	/**
	 * 服务添加/修改
	 */
	public function save()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			
			
			$params['service_ids']=','.implode(',',$params['service_ids']).',';
			
			$params['self_img']=implode(',',$params['self_img']);
			
			$params['id_card_img']=implode(',',$params['id_card_img']);
			
			$params['upd_time'] = time();
			
			if(!empty($params['uptel'])){
			    $params['upid']=db::name('user')->where('mobile',$params['uptel'])->value('id');
			    $params['bdtime']=time();
			}else{
			    $params['upid']=0;
			    $params['bdtime']=0;
			}
			
			unset($params['uptel']);
			
			$user_id=Db::name('techn')->where(['id'=>intval($params['id'])])->value('user_id');

			Db::name('techn')->where(['id'=>intval($params['id'])])->update($params);
			db::name('user')->where('id',$user_id)->update(['upid'=>$params['upid'],'bdtime'=>$params['bdtime']]);
			
		    team_get($params['upid']);
		
			//是否成功
			$result['code'] = 0;
			$result['msg'] = "操作成功";
		}
		echo json_encode($result);
	}

	/**
	 * 服务删除
	 */
	public function delete()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			
			if(Db::name('techn')->where(['id' => intval($params['id'])])->delete())
			{
				$result['code'] = 0;
				$result['msg'] = "删除成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "删除失败";
			}
		}
		echo json_encode($result);
	}
	
	/**
	 * 清除技师的公众号授权
	 */
	public function cash()
	{
	    $result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			$uid = Db::name('techn')->where(['id'=>intval($params['id'])])->value('user_id');
			
			$find_weixin = Db::name('user_weixin')->where(['user_id' =>$uid,'identity_type'=>'identity_type','client_type'=>'h5'])->find();
			if(empty($find_weixin)){
			    $result['code'] = -1;
				$result['msg'] = "公众号未授权，无法清除";
			}else{
			   if(Db::name('user_weixin')->where(['user_id' =>$uid,'identity_type'=>'techn','client_type'=>'h5'])->delete())
    			{
    				$result['code'] = 0;
    				$result['msg'] = "清除成功";
    			}else
    			{
    				$result['code'] = -1;
    				$result['msg'] = "清除失败";
    			} 
			}
			
		}
		echo json_encode($result);
	}
	
	
	
	
// 	public function yjindex()
// 	{
// 		$result['code'] = -1;
// 		$result['msg'] = "非法请求";
// 		if(request()->isPost())
// 		{
// 			$params = input();
// 			if(!empty($params['page']) && !empty($params['limit']))
// 			{
// 				$map = [];
// 				$map['upid']=['>',0];
				
				
// 				if(!empty($params['mobile']))
// 				{
// 				    $id=db::name('user')->where('mobile',$params['mobile'])->value('id');
// 				    $map['upid']=$id;
// 				}
// 				// print_r($map);exit;
				
// 				$count = Db::name('techn')->where($map)->count();
// 				$list_data = Db::name('techn')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
// 				if(!empty($list_data))
// 				{
// 					//遍历处理
// 					foreach($list_data as $key => $value)
// 					{
// 						$list_data[$key]['save_url'] = url('Techn/yjsaveinfo', ['id' => $value['id']]);
// 						//编辑url
// 						$list_data[$key]['sextitle'] = $value['sex']==1?'男':'女';
						
// 						$list_data[$key]['bdtime']=date('Y-m-d H:i:s',$value['bdtime']);
						
// 						$list_data[$key]['upname']=db::name('user')->where('id',$value['upid'])->value('nickname');
						
// 						$list_data[$key]['uptel']=db::name('user')->where('id',$value['upid'])->value('mobile');
// 					}
// 					$result['code'] = 0;
// 	        		$result['msg'] = "请求成功";
// 	        		$result['data'] = $list_data;
// 	        		$result['count'] = $count;
// 				}else
// 				{
// 					$result['code'] = -1;
// 	        		$result['msg'] = "暂无数据";
// 				}
// 			}
// 			echo json_encode($result);
// 		}else
// 		{
// 			return $this->fetch();
// 		}
// 	}
	
	public function yjindex()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [];
				// $map['upid']=['>',0];
				
				
				if(!empty($params['mobile']))
				{
				    $id=db::name('user')->where('mobile',$params['mobile'])->value('id');
				    $map['user_id']=$id;
				}
				// print_r($map);exit;
				
				$count = Db::name('user_bd')->where($map)->count();
				$list_data = Db::name('user_bd')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
					    
					    $user=db::name('user')->where('id',$value['user_id'])->find();
					    
					    $techn=db::name('techn')->where(['user_id'=>$value['bduid'],'upid'=>$value['user_id']])->find();
			    
        			    $users=db::name('user')->where(['id'=>$value['bduid'],'upid'=>$value['user_id']])->find();
        			    
        			    $value['bdtime']=!empty($techn)?$techn['bdtime']:$users['bdtime'];
        			    
						$list_data[$key]['save_url'] = url('Techn/yjsaveinfo', ['id' => $value['id']]);
						
						$list_data[$key]['bdtime']=date('Y-m-d H:i:s',$value['bdtime']);
						
						$list_data[$key]['upname']=$user['nickname'];
						
						$list_data[$key]['uptel']=$user['mobile'];
						
						$list_data[$key]['mobile']=!empty($techn)?$techn['mobile']:$users['mobile'];
						
						$list_data[$key]['nickname']=!empty($techn)?$techn['nickname']:$users['nickname'];
        			    
        				
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}
	
	
	
	
	public function yjsaveinfo()
	{
		// 参数
		$params = input();
		$info = [];
		
			//查询
		$info = Db::name('user_bd')->where(['id' => $params['id']])->find();
			
		$this->assign('info',$info);
		
		
		return $this->fetch();
	}

	/**
	 * 服务添加/修改
	 */
	public function yjsave()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			
			
			$params['upd_time'] = time();
			
			
			Db::name('user_bd')->where(['id'=>intval($params['id'])])->update($params);
			//是否成功
			$result['code'] = 0;
			$result['msg'] = "操作成功";
		}
		echo json_encode($result);
	}
}